package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.AgreedCarOfferEntity;
import com.sgb.kjwl.model.entity.transport.InquiryEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author : 熊丽
 * @date :2021/4/7 0007$ 9:44$
 * @Description :$
 */
public class OwnerAndDriverIViewModel extends AndroidViewModel {
    public MutableLiveData<ListResponse<List<InquiryEntity>>> ldRecord = new MutableLiveData<>();
    public MutableLiveData<ListResponse<List<InquiryEntity>>> DriverDateDetailData = new MutableLiveData<>();
    public MutableLiveData<ListResponse<List<InquiryEntity>>> OwnerDateDetailData = new MutableLiveData<>();
    public MutableLiveData<AgreedCarOfferEntity> InquirySuccess = new MutableLiveData<>();


    //
    private int page = 1;

    public OwnerAndDriverIViewModel(@NonNull Application application) {
        super(application);
    }

    public void getMyDriver(Context context,int status, boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        }
        DialogHelper.showProgressDialog(context, "数据加载中...");
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", 10);
        if (status != 0) {
            map.put("offerStatus", status);
        }
        NewAPIManager.getInstance().getLogisticsAPI().getoperateEnquiryList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<InquiryEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<InquiryEntity>>> data) {
                if (data.getData().haveMoreData()) {
                    page = data.getData().nextPage();
                }
                if (data!=null && data.getData()!=null){
                    DriverDateDetailData.postValue(data.getData());
                }
            }

            @Override
            public void onFail() {
                ldRecord.postValue(new ListResponse<>(false));
            }
        });
    }
    public void getMyOwner(Context context,int status, boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        }
        DialogHelper.showProgressDialog(context, "数据加载中...");
        Map<String, Object> map = new HashMap<>();
        if (UserOperating.getInstance().isCompany()){
            map.put("companyNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
        }
       if (UserOperating.getInstance().isTeam()){
           map.put("teamNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
       }
       if (UserOperating.getInstance().isPersonal()){
           map.put("terminal", 3);
       }
        map.put("page", page);
        map.put("limit", 10);
        if (status != 0) {
            map.put("offerStatus", status);
        }
        NewAPIManager.getInstance().getLogisticsAPI().getoperateMyInquiryList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<InquiryEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<InquiryEntity>>> data) {
                if (data.getData().haveMoreData()) {
                    page = data.getData().nextPage();
                }
                if (data!=null && data.getData()!=null){
                    OwnerDateDetailData.postValue(data.getData());
                }
            }

            @Override
            public void onFail() {
                ldRecord.postValue(new ListResponse<>(false));
            }
        });
    }
    public void GetagreedCarOffer(Context context,String no,String carNo,InquiryEntity item) {
        Map<String, Object> map = new HashMap<>();
        map.put("goodsSourceNo", no);
        map.put("carNo",carNo);
        DialogHelper.showProgressDialog(context, "数据加载中...");
        NewAPIManager.getInstance().getLogisticsAPI().checkCarStatues(map).enqueue(new Callback<BaseEntity<AgreedCarOfferEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AgreedCarOfferEntity>> call, Response<BaseEntity<AgreedCarOfferEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if ("ok".equals(response.body().getState())) {
                    InquirySuccess.postValue(response.body().getData());
                }else if ("400".equals(response.body().getCode())||"401".equals(response.body().getCode())){
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AgreedCarOfferEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }
}
